/*
 * MultipleFileFilter.java
 *
 * Created on September 15, 2007, 3:51 PM
 */

package picto.tools;

import java.io.File;
import java.util.*;

/**
 * Used when a single filter must accept multiple files.
 * @author davedes
 */
public class MultipleFileFilter extends AbstractFileFilter {
    
    /**
     * The list of extensions to accept.
     */
    private List<String> extList = new ExtensionList();
    
    /**
     * Creates a new instance of MultipleFileFilter with the specified description.
     * @param description the description to use
     */
    public MultipleFileFilter(String description) {
        super(description);
    }
    
    /**
     * Returns the extensions in the form of an array of Strings.
     * @return the extensions as an array
     */
    public String[] getExtensions() {
        return extList.toArray(new String[extList.size()]);
    }
    
    /**
     * Returns the extension list used by this filter.
     * @return the extension list
     */
    public List<String> getExtensionList() {
        return extList;
    }
    
    /**
     * Sets the extension list used by this filter. It is suggested that you use
     * MultipleFileFilter.ExtensionList to get the benefit of its toString() method.
     * @param extList the extension list
     */
    public void setExtensionList(List<String> extList) {
        this.extList = extList;
    }
    
    /**
     * Adds the specified extension to the list.
     * @param ext the extension, such as ".png"
     * @return <tt>true</tt> if the extension was added,
     *      <tt>false</tt> if not or if it already existed in the list
     */
    public boolean addExtension(String ext) {
        ext = ext!=null ? ext.toLowerCase() : null;
        if (ext!=null&&!extList.contains(ext)) {
            return extList.add(ext);
        } else 
            return false;
    }
    
    /**
     * Removes the specified extension from the list.
     * @param ext the extension to remove
     * @return <tt>true</tt> if the list changed as a result
     */
    public boolean removeExtension(String ext) {
        if (ext==null)
            return false;
        return extList.remove(ext.toLowerCase());
    }
        
    /**
     * Whether to accept the file for this filter.
     * @param f the file
     * @return true if we are accepting it
     */
    public boolean accept(File f) {
        if (f!=null) {
            if (f.isDirectory())
                return true;
            String name = f.getName().toLowerCase();
            for (int i=0; i<extList.size(); i++)
                if (name.endsWith(extList.get(i).toLowerCase()))
                    return true;
            return false;
        } else
            return false;
    }
    
    /**
     * A simple subclass of ArrayList that overrides toString() to 
     * output a more conventional list of extensions, such as:
     * <pre><code>.png;.jpg;.jpeg;.gif;.tiff</code></pre>
     */
    public class ExtensionList extends ArrayList<String> {
        
        /**
         * Returns a String representation of this list. Overridden for better output, such as:
         * <pre><code>.png;.jpg;.jpeg;.gif;.tiff</code></pre>
         * @return a string representation of this extension list
         */
        public String toString() {
            String full = "";
            for (int i=0; i<size(); i++) {
                full += this.get(i)+";";
            }
            //TODO: improve efficiency here by using if(i!=size()-1)
            if (full.endsWith(";")) 
                full = full.substring(0, full.length()-1);
            return full;
        }
    }
}
